package com.doubi.controller.sysDic;

import com.doubi.common.aspectj.annotation.SysLog;
import com.doubi.common.config.shiro.ShiroUtil;
import com.doubi.model.sys.TSysDicChild;
import com.doubi.model.sys.TSysUser;
import com.doubi.service.SysDicChildService;
import com.doubi.service.SysDicService;
import com.doubi.utils.DataUtil;
import com.doubi.vo.AppModel;
import com.doubi.vo.PageVo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("sysDicChild")
public class SysDicChildController {

    @Autowired
    private SysDicService sysDicService;
    @Autowired
    private SysDicChildService sysDicChildService;

    /**
     * 功能描述：进入字典子集的列表页面
     * @author   MrDoubi 2019-06-28 16:55
     * @param parentOid     父级Oid
     * @param parentCode    父级字典标识
     * @param model         Model
     * @return              返回页面路径
     */
    @RequiresPermissions("sysDicChild:list")
    @GetMapping("list")
    public String list(String parentOid,String parentCode, Model model){
        model.addAttribute("parentOid",parentOid);
        model.addAttribute("parentCode",parentCode);
        model.addAttribute("parent", sysDicService.getById(parentOid));
        return "web/sysDicChild/list";
    }

    /**
     * 功能描述：字典子级查询
     * @author   MrDoubi 2019-06-28 17:26
     * @param sysDicChild       TSysDicChild对象
     * @return                  Page<TSysDicChild>对象
     */
    @SysLog(title = "字典子级查询")
    @RequiresPermissions("sysDicChild:json")
    @PostMapping("json")
    @ResponseBody
    public PageVo<TSysDicChild> getPage(TSysDicChild sysDicChild){
        List<TSysDicChild> list = sysDicChildService.getAllByParentOid(sysDicChild.getParentOid());
        return new PageVo<TSysDicChild>(0,list.size(),list,"获取成功！");
    }

    /**
     * 功能描述：进入字典子集增加或修改页面
     * @author   MrDoubi 2019-06-29 08:57
     * @param sysDicChild       TSysDicChild对象
     * @param model             Model
     * @return                  返回页面路径
     */
    @RequiresPermissions(value = {"sysDicChild:edit", "sysDicChild:add"}, logical = Logical.OR)
    @GetMapping("edit")
    public String edit(TSysDicChild sysDicChild, Model model){
        if(DataUtil.isNotEmpty(sysDicChild.getOid())){
            sysDicChild = sysDicChildService.getById(sysDicChild.getOid());
        }
        model.addAttribute("bean",sysDicChild);
        return "web/sysDicChild/edit";
    }

    /**
     * 功能描述：字典表状态更新
     * @author   MrDoubi 2019-06-29 09:04
     * @param sysDicChild       TSysDicChild对象
     * @return                  返回AppModel对象
     */
    @SysLog(title = "字典表状态更新")
    @RequiresPermissions("sysDicChild:updateStatus")
    @PostMapping("updateStatus")
    @ResponseBody
    public AppModel updateStatus(TSysDicChild sysDicChild){
        TSysUser sysUser = ShiroUtil.getUser();
        return sysDicChildService.updateStatus(sysDicChild, sysUser);
    }

    /**
     * 功能描述：字典表子集删除
     * @author   MrDoubi 2019-06-29 10:08
     * @param sysDicChild       TSysDicChild对象
     * @return                  返回AppModel对象
     */
    @SysLog(title = "字典表子集删除")
    @RequiresPermissions("sysDicChild:delBean")
    @RequestMapping(value = "delBean",method = RequestMethod.POST)
    @ResponseBody
    public AppModel delBean(TSysDicChild sysDicChild){
        return sysDicChildService.delBean(sysDicChild);
    }

    /**
     * 功能描述：字典表子集保存或更新
     * @author   MrDoubi 2019-06-29 10:13
     * @param sysDicChild       TSysDicChild对象
     * @return                  返回AppModel对象
     */
    @SysLog(title = "字典表子集保存或更新")
    @RequiresPermissions("sysDicChild:saveOrUpdate")
    @PostMapping("saveOrUpdate")
    @ResponseBody
    public AppModel saveOrUpdate(TSysDicChild sysDicChild){
        TSysUser sysUser = ShiroUtil.getUser();
        return sysDicChildService.saveOrUpdate(sysDicChild, sysUser);
    }

}
